
package com.syu.loopcure.compensator.service.impl;

import com.syu.loopcure.compensator.entity.ToolCureCompensator;
import com.syu.loopcure.compensator.interfaces.ICompensatorRecord;
import com.syu.loopcure.compensator.model.CompensatorInfoModel;
import com.syu.loopcure.compensator.service.ToolCureCompensatorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**  
 * ClassName:CompensatorRecordImpl <br/>
 * Date:     2020年8月13日 上午9:18:37 <br/>  
 * @author   zhucun  
 * @since    JDK 1.8  
 */
@Service
public class CompensatorRecordImpl implements ICompensatorRecord {


    private final ToolCureCompensatorService toolCureCompensatorService;

    @Autowired
    public CompensatorRecordImpl(ToolCureCompensatorService toolCureCompensatorService){
        this.toolCureCompensatorService = toolCureCompensatorService;
    }

    @Override
    public List<CompensatorInfoModel> findRecordByUniqueKey(String md5Code) {
          
        List<ToolCureCompensator> toolCureCompensatorList = toolCureCompensatorService.findRecordByUniqueKey(md5Code);
        if(CollectionUtils.isEmpty(toolCureCompensatorList)) {
            return Collections.emptyList();
        }
        CompensatorInfoModel model = new CompensatorInfoModel();
        BeanUtils.copyProperties(toolCureCompensatorList.get(0), model);
        return Collections.singletonList(model);
    }

    @Override
    public void addRecord2Db(CompensatorInfoModel model) {
        ToolCureCompensator toolCureCompensator = new ToolCureCompensator();  
        BeanUtils.copyProperties(model, toolCureCompensator);
        toolCureCompensatorService.save(toolCureCompensator);
    }

    @Override
    public void updateRecord2Db(CompensatorInfoModel model) {
        ToolCureCompensator toolCureCompensator = new ToolCureCompensator();  
        BeanUtils.copyProperties(model, toolCureCompensator);
        
        toolCureCompensatorService.lambdaUpdate()
            .eq(ToolCureCompensator::getMd5Code, toolCureCompensator.getMd5Code())
            .update(toolCureCompensator);
        
    }
    

}
  
